package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson2.JSON;
import com.qingzhou.web.repository.entity.MaterialsEntity;
import com.qingzhou.web.service.impl.params.vo.excel.MaterialsExcelVo;

import java.util.List;
import java.util.stream.Collectors;

public class MaterialsConverter implements Converter<List<String>> {
    @Override
    public WriteCellData<?> convertToExcelData(List<String> value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if(CollUtil.isEmpty(value)) {
            WriteCellData<Object> emptyData = new WriteCellData<>("");
            emptyData.setType(CellDataTypeEnum.STRING);
            return emptyData;
        }

        return new WriteCellData<>(this.getConvertString(value));
    }

    public String getConvertString(List<String> value){
        if(CollUtil.isEmpty(value)) return "";

        String tpl = "{}-{}({}){}";
        List<String> collect = value.stream().map(item -> {
            MaterialsExcelVo vo = JSON.parseObject(item, MaterialsExcelVo.class);
            String pipeNum = StrUtil.isEmpty(vo.getPipeNum())?"":"-"+vo.getPipeNum() + "管";
            return StrUtil.format(tpl, vo.getName(), vo.getValue(), vo.getUnit(), pipeNum);
        }).collect(Collectors.toList());
        return CollUtil.join(collect,"\n");
    }
}
